package com.easy.util;

import cn.hutool.core.annotation.MirroredAnnotationAttribute;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;

import java.util.Map;

public class TokenUtil {
    //令牌
    public static final String TOKEN_KEY = "easy";
    //生成令牌
    public static String getToken(Integer id,String role) {
        Map map=Map.of(SYS.TOKEN_KEY_ID,id,SYS.TOKEN_KEY_ROLE,role);
        return JWTUtil.createToken(map,TOKEN_KEY.getBytes());
    }
    //解析令牌
    public static boolean verify(String token) {
        return JWTUtil.verify(token,TOKEN_KEY.getBytes());
    }
    //获取令牌中的数据
    public static Object getOption(String token,String key){
        return JWTUtil.parseToken(token).getPayload(key);
    }

    public static void main(String[] args) {
        String token = getToken(1,"admin");
        System.out.println(token);
        System.out.println(verify(token));
        System.out.println(getOption(token,SYS.TOKEN_KEY_ID));
        System.out.println(getOption(token,SYS.TOKEN_KEY_ROLE));
    }

}
